﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_EShop._Class.BLL.GZ;
using V5_Common;
using System.Data;
using V5_Model;

namespace V5_EShop.Admin.GZProductUser
{
    public partial class Admin_User : System.Web.UI.Page
    {
        #region 开始
        BLLGZUser bllGZProduct = new BLLGZUser();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string action = InputHelper.SetString(Request["action"]);
                Bind_DropClassList();
                switch (action)
                {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        this.Bind_DataList();
                        break;
                }
            }
        }

        private void Bind_DropClassList()
        {
            DataTable dt = new BLLGZUserClass().GetList("").Tables[0];
            this.ddlCustomTypeID.DataSource = dt;
            this.ddlCustomTypeID.DataTextField = "CustomTypeName";
            this.ddlCustomTypeID.DataValueField = "ID";
            this.ddlCustomTypeID.DataBind(); 
        }

        #region 增删改
        private void Bind_DataList()
        {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = bllGZProduct.GetList("*", Pager.PageSize, Pager.CurrentPageIndex, string.Empty, string.Empty, ref RecordCount).Tables[0];
            Pager.RecordCount = RecordCount;
            this.gvList.DataSource = dt;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(Guid ID)
        {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelGZUser model = new BLLGZUser().GetModel(ID);
            this.hfID.Value = model.ID.ToString();

            this.txtCustomNo.Text = model.CustomNo ?? "";
            this.txtCustomName.Text = model.CustomName ?? "";
            this.txtCustomShortName.Text = model.CustomShortName ?? "";
            this.txtCustomShortCutName.Text = model.CustomShortCutName ?? "";
            this.ddlCustomTypeID.SelectedIndex =
                this.ddlCustomTypeID.Items.IndexOf(
                    this.ddlCustomTypeID.Items.FindByValue(model.CustomTypeID.ToString())
                );

            this.ddlPriceSet.SelectedIndex =
                this.ddlPriceSet.Items.IndexOf(
                    this.ddlPriceSet.Items.FindByValue(model.CustomSaleType.ToString())
                ); 
            this.txtRecommendedID.Text = model.RecommendedID.ToString();
            this.txtCustomSaleType.Text = model.CustomSaleType.ToString();
            this.txtAddress.Text = model.Address ?? "";
            this.txtPOINT.Text = model.POINT.ToString();
            this.txtDiscount.Text = model.Discount.ToString();
            this.txtPostalCode.Text = model.PostalCode ?? "";
            this.txtMobilePhone.Text = model.MobilePhone ?? "";
            this.txtTelePhone.Text = model.TelePhone ?? "";
            this.txtFaxNo.Text = model.FaxNo ?? "";
            this.txtSex.Text = model.Sex ?? "";
            this.txtEmail.Text = model.Email ?? "";
            this.txtWebsite.Text = model.Website ?? "";
            this.txtTaxNo.Text = model.TaxNo ?? "";
            this.txtBankNo.Text = model.BankNo ?? "";
            this.txtBank.Text = model.Bank ?? "";
            this.txtLinkMan.Text = model.LinkMan ?? "";
            this.txtAccountReceivable.Text = model.AccountReceivable.ToString();
            this.txtFirstAccountReceivable.Text = model.FirstAccountReceivable.ToString();
            this.txtCardNo.Text = model.CardNo ?? "";
            this.txtExistingIntegral.Text = model.ExistingIntegral.ToString();
            this.txtConsumptionIntegral.Text = model.ConsumptionIntegral.ToString();
            this.txtResidualIntegral.Text = model.ResidualIntegral.ToString();
            this.txtCreditMoney.Text = model.CreditMoney.ToString();
            this.chkIsUseCredit.Checked = model.IsUseCredit.Value;
            this.txtCreditDay.Text = model.CreditDay.ToString();
            this.txtCreditStartDate.Text = model.CreditStartDate.ToString();
            this.txtCreditEndDate.Text = model.CreditEndDate.ToString();
            this.txtBirthday.Text = model.Birthday.ToString();
            this.txtAdvanceLastMoney.Text = model.AdvanceLastMoney.ToString();
            //this.txtPwd.Text = model.Pwd.ToString();
            this.chkIsSMS.Checked = model.IsSMS.Value;
            this.txtSellerID.Text = model.SellerID.ToString();
            this.ddlDisable.SelectedIndex =
                this.ddlDisable.Items.IndexOf(
                    this.ddlDisable.Items.FindByValue(model.Disable.ToString())
                );
            this.txtRemark.Text = model.Remark ?? "";
            this.btnSave.Text = "保存修改";
        }

        private void Bind_Delete(Guid id)
        {
            bllGZProduct.Delete(id);
            Bind_DataList();
        }
        #endregion



        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string id = this.hfID.Value;

            string CustomNo = this.txtCustomNo.Text;
            string CustomName = this.txtCustomName.Text;
            string CustomShortName = this.txtCustomShortName.Text;
            string CustomShortCutName = this.txtCustomShortCutName.Text;
            string CustomTypeID = this.ddlCustomTypeID.SelectedValue;
            string RecommendedID = this.txtRecommendedID.Text;
            int CustomSaleType = InputHelper.SetNumber(this.ddlPriceSet.SelectedValue);
            string Address = this.txtAddress.Text;
            int POINT = InputHelper.SetNumber(this.txtPOINT.Text);
            decimal Discount = decimal.Parse(this.txtDiscount.Text == "" ? "0" : this.txtDiscount.Text);
            string PostalCode = this.txtPostalCode.Text;
            string MobilePhone = this.txtMobilePhone.Text;
            string TelePhone = this.txtTelePhone.Text;
            string FaxNo = this.txtFaxNo.Text;
            string Sex = this.txtSex.Text;
            string Email = this.txtEmail.Text;
            string Website = this.txtWebsite.Text;
            string TaxNo = this.txtTaxNo.Text;
            string BankNo = this.txtBankNo.Text;
            string Bank = this.txtBank.Text;
            string LinkMan = this.txtLinkMan.Text;
            decimal AccountReceivable = decimal.Parse(this.txtAccountReceivable.Text);
            decimal FirstAccountReceivable = decimal.Parse(this.txtFirstAccountReceivable.Text);
            string CardNo = this.txtCardNo.Text;
            decimal ExistingIntegral = decimal.Parse(this.txtExistingIntegral.Text == "" ? "0" : this.txtExistingIntegral.Text);
            decimal ConsumptionIntegral = decimal.Parse(this.txtConsumptionIntegral.Text == "" ? "0" : this.txtConsumptionIntegral.Text);
            decimal ResidualIntegral = decimal.Parse(this.txtResidualIntegral.Text == "" ? "0" : this.txtResidualIntegral.Text);
            decimal CreditMoney = decimal.Parse(this.txtCreditMoney.Text == "" ? "0" : this.txtCreditMoney.Text);
            bool IsUseCredit = chkIsUseCredit.Checked;
            int CreditDay = InputHelper.SetNumber(this.txtCreditDay.Text);
            DateTime? CreditStartDate = null;
            try
            {
                CreditStartDate = Convert.ToDateTime(this.txtCreditStartDate.Text);
            }
            catch { }
            DateTime? CreditEndDate = null;
            try
            {
                CreditEndDate = Convert.ToDateTime(this.txtCreditEndDate.Text);
            }
            catch { }

            DateTime? Birthday = null;

            try
            {
                Birthday = Convert.ToDateTime(this.txtBirthday.Text);
            }
            catch { }
            DateTime CreateDate = DateTime.Now;
            decimal AdvanceLastMoney = decimal.Parse(this.txtAdvanceLastMoney.Text == "" ? "0" : this.txtAdvanceLastMoney.Text);
            string Pwd = this.txtPwd.Text;
            bool IsSMS = chkIsSMS.Checked;
            string SellerID = this.txtSellerID.Text;
            bool Disable = InputHelper.SetNumber(this.ddlDisable.SelectedValue) == 1 ? true : false;
            string Remark = this.txtRemark.Text;
            ModelGZUser model = new ModelGZUser();
            model.ID = new Guid(id == "0" ? "00000000-0000-0000-0000-000000000000" : id);
            model.CustomNo = CustomNo;
            model.CustomName = CustomName;
            model.CustomShortName = CustomShortName;
            model.CustomShortCutName = CustomShortCutName;
            model.CustomTypeID = new Guid(CustomTypeID == "0" ? "00000000-0000-0000-0000-000000000000" : CustomTypeID);
            model.RecommendedID = new Guid(RecommendedID == "0" ? "00000000-0000-0000-0000-000000000000" : RecommendedID);
            model.CustomSaleType = CustomSaleType;
            model.Address = Address;
            model.POINT = POINT;
            model.Discount = Discount;
            model.PostalCode = PostalCode;
            model.MobilePhone = MobilePhone;
            model.TelePhone = TelePhone;
            model.FaxNo = FaxNo;
            model.Sex = Sex;
            model.Email = Email;
            model.Website = Website;
            model.TaxNo = TaxNo;
            model.BankNo = BankNo;
            model.Bank = Bank;
            model.LinkMan = LinkMan;
            model.AccountReceivable = AccountReceivable;
            model.FirstAccountReceivable = FirstAccountReceivable;
            model.CardNo = CardNo;
            model.ExistingIntegral = ExistingIntegral;
            model.ConsumptionIntegral = ConsumptionIntegral;
            model.ResidualIntegral = ResidualIntegral;
            model.CreditMoney = CreditMoney;
            model.IsUseCredit = IsUseCredit;
            model.CreditDay = CreditDay;
            model.CreditStartDate = CreditStartDate;
            model.CreditEndDate = CreditEndDate;
            model.Birthday = Birthday;
            model.CreateDate = CreateDate;
            model.AdvanceLastMoney = AdvanceLastMoney;
            model.Pwd = Pwd;
            model.IsSMS = IsSMS;
            model.SellerID = new Guid(SellerID == "0" ? "00000000-0000-0000-0000-000000000000" : SellerID);
            model.Disable = Disable;
            model.Remark = Remark;
            if (id == "0")
            {
                model.ID = Guid.NewGuid();
                new BLLGZUser().Add(model);
            }
            else
            {
                model.ID = new Guid(id);
                new BLLGZUser().Update(model);
            }
            this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Guid id = new Guid(Easp.Str.SetString(e.CommandArgument));
            string eName = Easp.Str.SetString(e.CommandName).ToLower();
            switch (eName)
            {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
            }
        }

        #endregion 结束
    }
}